package
{
	/**
	 * @author kings
	 * @E-mail: qywangweiqiang@126.com
	 * 2012-11-30上午12:28:05
	 * */
	import event.UIEditsEventName;

	import fl.controls.DataGrid;
	import fl.controls.dataGridClasses.DataGridColumn;
	import fl.data.DataProvider;
	import fl.events.DataGridEvent;

	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.utils.describeType;

	import kingsAsLib.events.EventManager;
	import kingsAsLib.events.KEvents;

	import save.NameWholeArrStructure;

	public class PropertyUI extends Sprite
	{
		static private var _propertyUI:PropertyUI=new PropertyUI();

		private var bgLayer:Sprite;

		private var _currentInfo:DataGrid;

		public function PropertyUI()
		{
			super();

			bgLayer=new Sprite();
			addChild(bgLayer);


			var col1:DataGridColumn=new DataGridColumn('Name');
			col1.editable=false;
			var col2:DataGridColumn=new DataGridColumn('Value')
			col2.editable=true;

			_currentInfo=new DataGrid();
			_currentInfo.move(0, 0);
			_currentInfo.setSize(240, 500);
			_currentInfo.editable=true;
			_currentInfo.addColumn(col1);
			_currentInfo.addColumn(col2);
			_currentInfo.addEventListener(DataGridEvent.ITEM_EDIT_END, itemEditEndHandler);
			this.addChild(_currentInfo);

			initListen();
		}

		protected function itemEditEndHandler(event:DataGridEvent):void
		{
			var data:Object=_currentInfo.itemEditorInstance.data;
			if (data == null)
				return;
			var Name:String=data.Name;
			var target:DisplayObject=data.target;
			var valueType:String=data.valueType;
			var newValue:String=_currentInfo.itemEditorInstance.text;
			var value:*;
			if (valueType == 'wholeVariable')
			{
				if(newValue=="null"){
					return;
				}
				value=newValue;

				var nameWhole:NameWholeArrStructure=new NameWholeArrStructure(value, target.name);
				EventManager.dispatchEvent(new KEvents(UIEditsEventName.variableWholeChange, nameWhole));
				return;
			}
			else if (valueType == 'Number')
			{
				value=Number(newValue);
			}
			else if (valueType == 'Boolean')
			{
				value=newValue == 'true' ? true : false;
			}
			else if (valueType == 'String')
			{
				value=newValue;
			}

			target[Name]=value;

			try
			{
				target['draw']();
			}
			catch (e:*)
			{

			}
		}

		private function initListen():void
		{
			// 
			EventManager.addEventListener(UIEditsEventName.chooseObj, showChooseObjProperty);
		}

		private function showChooseObjProperty(e:KEvents):void
		{
			// 
			var dis:DisplayObject=e.getData();
			if (dis)
			{

				_currentInfo.dataProvider=getVariableKeyAndValue(dis);
			}
		}


		private function getVariableKeyAndValue(_target:*):DataProvider
		{
			var type:XML=describeType(_target);
			var accessors:XMLList=type.accessor;

			var classNameStr:String=type.attribute("name");

			var dp:DataProvider=new DataProvider();
			for each (var item:* in accessors)
			{
				var n:String=item.@name;
				var a:String=item.@access;
				var t:String=item.@type;
				if (a == 'readwrite' && editTest(t) && n != 'name')
					dp.addItem({Name:n, Value:_target[n],target:_target,valueType:t});
			}
			//名字
			dp.addItem({Name:"name", Value:_target["name"],target:_target,valueType:"String"});
			dp.addItem({Name:"private  public", Value:"null",target:_target,valueType:"wholeVariable"});

			return dp;
		}

		private function editTest(type:String):Boolean
		{
			if (type == 'Number' || type == 'Boolean' || type == 'String')
			{
				return true;
			}
			return false;
		}


		static public function instance():PropertyUI
		{
			return _propertyUI;
		}

		public function setBgSize(_w:int=250, _h:int=500):void
		{
			bgLayer.graphics.clear();
			bgLayer.graphics.beginFill(0x66CCFF, 0.3);
			bgLayer.graphics.drawRect(0, 0, _w, _h);
			bgLayer.graphics.endFill();
		}

	}

	//end class
}
